package com.lunwen.lunwen.controller;

import com.lunwen.lunwen.bean.Result;
import com.lunwen.lunwen.bean.XueYuan;
import com.lunwen.lunwen.bean.ZhuanYe;
import com.lunwen.lunwen.services.XueYuanService;
import com.lunwen.lunwen.services.ZhuanYeService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

/**
 * (ZhuanYe)表控制层
 *
 * @author makejava
 * @since 2020-07-20 13:22:26
 */
@RestController
@RequestMapping("api")
public class ZhuanYeController {

    /**
     * 服务对象
     */
    @Autowired
    private ZhuanYeService zhuanYeService;

    @Autowired
    private XueYuanService xueYuanService;

    @RequestMapping("selectonezhuanye")
    public Result selectOneZhuanYe(@RequestParam("mingCheng") String mingCheng){
        System.out.println(mingCheng);
        Result result = new Result();
        result.setResult("200","查询成功！");
        result.setData(zhuanYeService.queryByMingCheng(mingCheng));
        return result;
    }

    @RequestMapping("selectallzhuanye")
    public Result selectAllZhuanYe(@RequestParam("xueyuanId") String xueyuanId){
        Result result = new Result();
        if (StringUtils.isEmpty(xueyuanId)){
            result.setResult("201","学院ID不能为空！");
            return result;
        }
        ZhuanYe zhuanYe = new ZhuanYe();
        zhuanYe.setXueyuanId(xueyuanId);
        result.setResult("200","查询成功！");
        result.setData(zhuanYeService.queryAll(zhuanYe));
        return result;
    }

    @RequestMapping("insertzhuanye")
    public Result insertZhuanYe(@RequestBody ZhuanYe zhuanYe){
        Result result = new Result();
        if (StringUtils.isEmpty(zhuanYe.getZhuanyemingcheng())){
            result.setResult("201","专业名称不能为空！");
            return result;
        }
        if(zhuanYeService.queryByMingCheng(zhuanYe.getZhuanyemingcheng()) != null){
            result.setResult("201","该专业已存在！");
            return result;
        }
        if (StringUtils.isEmpty(zhuanYe.getCengci())){
            result.setResult("201","层次不能为空！");
            return result;
        }
        if (StringUtils.isEmpty(zhuanYe.getXueyuanId())){
            result.setResult("201","学院ID不能为空！");
            return result;
        }
        if (zhuanYe.getCengci() != 0 && zhuanYe.getCengci() != 1){
            result.setResult("201","层次数据不正确！");
            return result;
        }
        if(xueYuanService.queryById(zhuanYe.getXueyuanId()) == null){
            result.setResult("201","不存在该学院！");
            return result;
        }
        String id = UUID.randomUUID().toString();
        zhuanYe.setId(id);
        zhuanYeService.insert(zhuanYe);
        result.setResult("200","新增成功！",id);
        return result;
    }

}